package com.minotauro.bean.property;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.processing.AbstractProcessor;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import javax.tools.JavaCompiler.CompilationTask;

import org.apache.commons.lang.SystemUtils;

import com.minotauro.i18n.base.BaseI18NMain;
import com.minotauro.i18n.scanner.Visitor;

public class BeanVisitor implements Visitor {

  private BaseI18NMain app;

  private BeanIsProcessable isProcesable;
  private File output;

  // --------------------------------------------------------------------------------

  public BeanIsProcessable getIsProcesable() {
    return isProcesable;
  }

  public void setIsProcesable(BeanIsProcessable isProcesable) {
    this.isProcesable = isProcesable;
  }

  // --------------------------------------------------------------------------------

  public File getOutput() {
    return output;
  }

  public void setOutput(File output) {
    this.output = output;
  }

  // --------------------------------------------------------------------------------

  public BeanVisitor(BeanIsProcessable isProcessable, File output) {
    this.isProcesable = isProcessable;
    this.output = output;
  }

  // --------------------------------------------------------------------------------

  @Override
  public void visit(File file, List<String> path) {

    JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();

    StandardJavaFileManager standardJavaFileManager = //
    javaCompiler.getStandardFileManager(null, null, null);

    List<File> fileList = new ArrayList<File>();
    fileList.add(file);

    Iterable<? extends JavaFileObject> compilationUnits = //
    standardJavaFileManager.getJavaFileObjectsFromFiles(fileList);

    CompilationTask task = javaCompiler.getTask( //
        null, standardJavaFileManager, null, null, null, compilationUnits);

    GetterSetterProcessor getterSetterProcessor = new GetterSetterProcessor();

    List<AbstractProcessor> processorList = new ArrayList<AbstractProcessor>();
    processorList.add(getterSetterProcessor);

    task.setProcessors(processorList);

    task.call();

    String outputDirectory = output.getPath() + SystemUtils.FILE_SEPARATOR;
    String pckName = pathListToPackName(path);
    app = new BaseI18NMain();

    try {
      app.prop(getterSetterProcessor.getPropertyBeanList(), file.getName()
          .replace(".java", ""), outputDirectory, pckName);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  // --------------------------------------------------------------------------------

  public String pathListToPathName(List<String> pathList) {
    String path = "";

    for (int i = 0; i < pathList.size(); i++) {
      path = path + pathList.get(i);
      path = path + SystemUtils.FILE_SEPARATOR;
    }
    return path;
  }

  // --------------------------------------------------------------------------------

  public String pathListToPackName(List<String> pathList) {
    String pack = "";

    for (int i = 0; i < pathList.size(); i++) {
      pack = pack + pathList.get(i);
      if (i < pathList.size() - 1) {
        pack = pack + ".";
      }
    }
    return pack;
  }
}